import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import bus from "./utils/bus"
Vue.prototype.$bus = bus
//引入element-ui
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
//引入全局css和element-rest
import "@/assets/styles/base.css"
import "@/assets/styles/el-reset.css"
//引入iconfont
import "./assets/iconfont/iconfont.css"
//引入subMenu组件
import qfSubMenu from "qf-sub-menu"
//导入 nprogress 进度条
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'


// Vue.config.productionTip = false;
Vue.use(ElementUI);
Vue.use(qfSubMenu) //注入全局
//按需引入

// import {Carousel,CarouselItem} from "element-ui"

//全局注册组件
// Vue.component("el-carousel",Carousel)
// Vue.component("el-carousel-item",CarouselItem)

//路由前置钩子(导航守卫)
router.beforeEach((to,from,next)=>{ 
  // NProgress.start(); //进度条出现
  NProgress.set(0.5);//设置进度条的进度
//  console.log(to);
//  console.log(from);
//用户登入之后，localstorage中有token 
let token = localStorage.getItem("ht2006-token") || null;
console.log(token);
if(token){
  //如果是注册页面或者是登入页面，直接放行
 if(store.state.menuList.length==0){
   //说明没有用户名路由，触发action获取用户路由
   store.dispatch("FETCH_MENULIST")
   .then(()=>{
     next({path :to.path})
     //这里要注意，next里面要传参数既要进入的页面的路由信息，因为next传参数后,
     //当前要进入的路由会被废止，转而进入参数对应的路由，虽然是同一个路由，这么做主要是为了确保addRoutes失效
   })
 }else{
   next()
 }

}else{ //没token
if(to.path ==="/login"){
  next()
}else{
  //访问的不是登入页，就要跳转到登入页
  next({path:"/login"})
}
}
})

//使用路由后置钩子处理面包屑
router.afterEach((to,from)=>{
  console.log(to);
  let crumblist = to.matched.slice(1)
  console.log(crumblist);
  store.commit("SET_CRUMEBS",crumblist)
  NProgress.done();  //进度条消失
})


import "./utils/recursionRoutes.js"

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount("#app");
